package com.fingard.dsp.bank.directbank.boc01;

import java.math.BigDecimal;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IExchangeRate;
import com.fingard.dsp.bank.reqrespobj.exchangerate.ReqExchangeRateQry;
import com.fingard.dsp.bank.reqrespobj.exchangerate.RetAtsExchangeRateQry;
import com.fingard.dsp.bank.reqrespobj.exchangerate.detail.RetAtsExchangeRateDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;


public class BOC01ExchangeRate extends BOC01Base implements IExchangeRate {
    @Override
    public RetAtsExchangeRateQry exchangeRate(ReqExchangeRateQry p_reqrate) throws Exception {
        RetAtsExchangeRateQry retAts = new RetAtsExchangeRateQry(p_reqrate.reqHeader);
        RetAtsExchangeRateDetail tmpRetDetail = new RetAtsExchangeRateDetail(p_reqrate.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            while (p_reqrate.readRecord()) {
                BOC01Header bankHeader = new BOC01Header();
                bankHeader.trncod = "b2e0226";//交易代码
                bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("trn-b2e0226-rq");
                xmlWriter.writeStartElement("b2e0226-rq");
                xmlWriter.writeElementString("curcde", convertCurToBank(p_reqrate.readedDetail.sourceCur));
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                //模拟返回数据
            /*tmpStrRet[0] = "";
            tmpStrRet[1] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<bocb2e version=\"120\" locale=\"zh_CN\">\n" +
                    "\t<head>\n" +
                    "\t\t<termid>E022011099095</termid>\n" +
                    "\t\t<trnid>04051003129</trnid>\n" +
                    "\t\t<custid>10095720</custid>\n" +
                    "\t\t<cusopr>b2egaoy</cusopr>\n" +
                    "\t\t<trncod>b2e0226</trncod>\n" +
                    "\t\t<token>24BDF753CF83EE57B25BD3924189D5F7</token>\n" +
                    "\t</head>\n" +
                    "\t<trans>\n" +
                    "\t\t<trn-b2e0226-rs>\n" +
                    "\t\t<status>\n" +
                    "\t\t<rspcod>B001</rspcod>\n" +
                    "\t\t<rspmsg>模拟返回成功</rspmsg>\n" +
                    "\t\t</status>\n" +
                    "\t\t\t<b2e0226-rs>\n" +
                    "\t\t\t\t<status>\n\n" +
                    "\t\t\t\t<rspcod>B001</rspcod>\n\n" +
                    "\t\t\t\t<rspmsg>模拟汇率成功</rspmsg>\n\n" +
                    "\t\t\t\t</status>\n\n" +
                    "\t\t\t\t<buyrate>123.0011</buyrate>\n" +
                    "\t\t\t\t<sellrate>313.0012</sellrate>\n" +
                    "\t\t\t\t<buynote>85.0013</buynote>\n" +
                    "\t\t\t\t<sellnote>77.1251</sellnote>\n" +
                    "\t\t\t\t<spotrate>6.1401</spotrate>\n" +
                    "\t\t\t</b2e0226-rs>\n" +
                    "\t\t</trn-b2e0226-rs>\n" +
                    "\t</trans>\n" +
                    "</bocb2e> ";*/
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();

                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                    if (tmpBkReader != null) {
                        String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0226-rs/status/rspcod");
                        String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0226-rs/status/rspmsg");
                        if (isActiveRetCode(tmprspcod)) {
                            while (tmpBkReader.read("/bocb2e/trans/trn-b2e0226-rs/b2e0226-rs")) {

                                if (tmpBkReader.readedXElement.getSingleChild("status") != null) {
                                    String tmpEachRspcod = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspcod");
                                    String tmpEachRspmsg = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspmsg");
                                    if (tmpEachRspcod.equals("B001")) {
                                        tmpRetDetail.respCode = "0";
                                        tmpRetDetail.respInfo = tmpEachRspmsg;
                                        tmpRetDetail.buyrate = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("buyrate")).divide(new BigDecimal(100)));
                                        tmpRetDetail.sellrate = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("sellrate")).divide(new BigDecimal(100)));
                                        tmpRetDetail.buynote = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("buynote")).divide(new BigDecimal(100)));
                                        tmpRetDetail.sellnote = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("sellnote")).divide(new BigDecimal(100)));
                                        tmpRetDetail.rate = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("spotrate")).divide(new BigDecimal(100)));
                                        tmpRetDetail.rateDate = Format.DateTimeFormat.format(new Date());
                                    } else {
                                        tmpRetDetail.respCode = "-1";
                                        tmpRetDetail.respInfo = tmpEachRspmsg;
                                        if (StringHelper.isNullOrEmpty(tmpRetDetail.respInfo)) {
                                            tmpRetDetail.respInfo = tmpEachRspcod;
                                        }
                                    }
                                } else {
                                    tmpRetDetail.respCode = "-1";
                                    tmpRetDetail.respInfo = "status节点为空";
                                }

                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                                tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                            }
                            tmpRetDetail.respInfo = tmprspmsg;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            }
        } catch (Exception e) {
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = LogHelper.getStackTrace(e);
            e.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetDetail);
        retAts.writeEnd();
        return retAts;
    }
}
